<?php

  /**
   * MVC Engine Model implements some generic methods and objects to run with an
   * external engine.<br />
   * Use it and inherits all your MVC Engine Model classes from this one.
   *
   * @author D. Mottier
   * @version 2.0
   */
  abstract class EngineModel extends Model {

    /**
     * The action to execute by the engine on the model.
     * @var IString The action.
     */
    private $action = null;

    /**
     * The error message raised by the engine.
     * @var Istring The error.
     */
    private $error = null;

    /**
     * Arraylist of arguments givable by the front end to the engine.
     * @var IArrayList The arguments collection.
     */
    public $arguments = null;

    /**
     * Initialize the arguments collection.
     */
    public function __construct(array $args = null) {
      parent::__construct();

      if (!is_null($args)) {
        $this->arguments = new ArrayList($args);
      } else {
        $this->arguments = new ArrayList();
      }
    }

    /**
     * Checks if the argument exists.
     * @return IBoolean Returns TRUE if the argument exists, FALSE otherwise.
     * @link http://us3.php.net/manual/en/function.empty.php
     */
    public function argumentExists(IString $key) {
      return new Boolean($this->arguments->keyExists($key->toString()));
    }

    /**
     * Return the action to apply on the model by the engine.
     * @return IString The action.
     */
    public function getAction() {
      return $this->action;
    }

    /**
     * Counts the number of arguments.
     * @return IInteger The number of arguments.
     * @link http://be2.php.net/manual/en/function.count.php
     */
    public function getArgumentsLength() {
      return new Integer(count($this->arguments));
    }

    /**
     * Returns the error.
     * @return IString The error.
     */
    public function getError() {
      return $this->error;
    }

    /**
     * Returns the argument located at the specified key.
     * @param IString $key The argument name.
     * @return IString The value located at the specified key.
     * @link http://fr2.php.net/manual/en/function.array-key-exists.php
     * @see NoSuchElementException
     */
    public function getArgument(IString $key) {
      if ($this->arguments->keyExists($key->toString())) {
        return new String($this->arguments->getAtIndex($key->toString()));
      } else {
        throw new NoSuchElementException("The key " . $key . "does not exists.");
      }
    }

    /**
     * Returns TRUE if there is one or more arguments.
     * @return IBoolean TRUE if there is one or more arguments.
     * @link http://us3.php.net/manual/en/function.empty.php
     */
    public function hasArgument() {
      return $this->arguments->getLength() > 0 ? new Boolean(true) : new Boolean(false);
    }

    /**
     * Return True if there is an error.<br />
     *
     * @return boolean True if there is an error.<br /><br />
     *
     * @see <a href="http://us3.php.net/manual/en/function.empty.php">empty()</a>
     */
    public function hasError() {
      if (!is_null($this->error)) {
        return new Boolean(!$this->error->isEmpty()->booleanValue());
      } else {
        return new Boolean(false);
      }
    }

    /**
     * Set the action to execute on the model.<br />
     *
     * @param string $value The method name to execute on the engine.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.empty.php">empty()</a>
     * @see <a href="http://be2.php.net/manual/en/function.is-string.php">is_string()</a>
     * @see IllegalArgumentException
     */
    public function setAction(IString $value) {
      if (!$value->isEmpty()->booleanValue()) {
        $this->action = $value;
      }
    }

    /**
     * Set a new argument for an engine call.<br />
     *
     * @param string $key The key to access the value.
     * @param string $value The value located at the specified key.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.array-key-exists.php">array_key_exists()</a>
     * @see <a href="http://be2.php.net/manual/en/function.is-string.php">is_string()</a>
     * @see IllegalArgumentException
     */
    public function setArgument(IString $key, IString $value) {
      $this->arguments->add($value->toString(), $key->toString());
    }

    /**
     * Set an error text for an engine call.<br />
     *
     * @param string $value The error text.
     *
     * @see <a href="http://be2.php.net/manual/en/function.is-string.php">is_string()</a>
     * @see IllegalArgumentException
     */
    public function setError(IString $value) {
      if (!$value->isEmpty()->booleanValue()) {
        $this->error = $value;
      }
    }

  }

?>
